//
//  XHStoreViewController.m
//  testApp
//
//  Created by ColdMountain on 2017/6/15.
//  Copyright © 2017年 ColdMountain. All rights reserved.
//

#import "XHStoreViewController.h"

#import "XHPlayerCell.h"

#define SCREEN_CONTENT_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_CONTENT_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface XHStoreViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray *imageArray;
@property (strong, nonatomic) UIWebView *ui_webView;
@end

@implementation XHStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageArray=@[@"http://image.aiwannet.com/43414fef1faa4bce93ea2b7f157aaea4.jpg",
                       @"http://image.aiwannet.com/a0eb74cb649245629cc37080e4d51025.jpg",
                       @"http://image.aiwannet.com/5ad4eb017a3c4be9b708c5db28ef3dc4.jpg",
                       @"http://image.aiwannet.com/19b61e7543bc448fb94180d3b9398ec3.jpg",
                       @"http://image.aiwannet.com/04879b1808884db1af310255a112ec70.jpg",
                       @"http://image.aiwannet.com/ae7107121f1547a592e71384bfff3727.jpg",
                       @"http://image.aiwannet.com/fa008549ef604b32bec0a3284643c919.jpg",
                       @"http://image.aiwannet.com/a28ab8af0e4b429bb40730a11a8b93f7.jpg"];
    [self initWebHeadView];
    [self loadWebView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHPlayerCell" bundle:nil] forCellReuseIdentifier:XHPlayerCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置webView
-(void)initWebHeadView{
    self.ui_webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_CONTENT_WIDTH, 0.1)];
    self.ui_webView.scrollView.alwaysBounceHorizontal = YES;
    self.ui_webView.scrollView.alwaysBounceVertical = YES;
    [self.ui_tableView setTableHeaderView:self.ui_webView];
    //监听
    [self.ui_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

//加载webView
-(void)loadWebView{
//    NSString*utf = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString*htmlString = [NSString stringWithFormat:@"<html> \n"
//                           "<head> \n"
//                           "<style type=\"text/css\"> \n"
//                           "body {font-size:15px;font-family:PingFangSC-Light;}\n"
//                           "img{max-width: %fpx;height: auto;}"
//                           "</style> \n"
//                           "</head> \n"
//                           "<body> \n%@ \n"
//                           "</body>"
//                           "</html>",self.view.frame.size.width-20,utf];
//    UIImage *selectedImage = [UIImage imageNamed:@"sucai"];
//    NSString *stringImage = [self htmlForJPGImage:selectedImage];
    NSString*str=@"<img src=\"http://upload.techweb.com.cn/2017/0106/1483690985396.jpg\"/><img src=\"http://image.aiwannet.com/a0eb74cb649245629cc37080e4d51025.jpg\"/><img src=\"http://image.aiwannet.com/fa008549ef604b32bec0a3284643c919.jpg\"/>";
    //构造内容
    NSString *contentImg = [NSString stringWithFormat:@"%@", str];
    NSString *content =[NSString stringWithFormat:
                        @"<html>"
                        "<style type=\"text/css\">"
                        "<!--"
                        "body {font-size:15px;font-family:PingFangSC-Light;}\n"
                        "img{max-width:%fpx;height: auto;}"
                        "</style>"
                        "<body>"
                        "%@"
                        "</body>"
                        "</html>",
                        SCREEN_CONTENT_WIDTH-20,
                        contentImg];
    
    //让self.contentWebView加载content
    [self.ui_webView loadHTMLString:content
                            baseURL:nil];
}
- (NSString *)htmlForJPGImage:(UIImage *)image
{
    NSData *imageData = UIImageJPEGRepresentation(image,1.0);
    NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64Encoding]];
    return [NSString stringWithFormat:@"<img src = \"%@\" />", imageSource];
}


//监听webView.scrollView.contentSize
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"contentSize"]) {
        //延迟0.1s解决第一次计算的误差
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
            CGRect frame = self.ui_webView.frame;
            frame.size = [[change objectForKey:NSKeyValueChangeNewKey] CGSizeValue];
            self.ui_webView.frame=CGRectMake(0, 0, SCREEN_CONTENT_WIDTH, frame.size.height);
            self.ui_tableView.tableHeaderView=nil;
            [self.ui_tableView setTableHeaderView:self.ui_webView];
        });
    }
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHPlayerCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHPlayerCellIdentifier];
    return cell;
}

#pragma - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=150;
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    return height;
}



@end
